package com.yupi.yuaiagent.tool;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import com.yupi.yuaiagent.constant.FileConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

public class FileOperationTool {

    private final String SAVE_FILE_DIR = FileConstant.FILE_SAVE_DIR + "\\file";

    /**
     * fileName 文件名称
     * @return
     */
    @Tool(description = "读取文件的内容")
    public String read(@ToolParam(description = "文件名称") String fileName){
        String filePath = SAVE_FILE_DIR + "\\" + fileName;
        try {
            return FileUtil.readUtf8String(filePath);
        } catch (IORuntimeException e) {
            return "Error reading file：" + e.getMessage();
        }
    }

    /**
     * 写入文件
     * @return
     */
    @Tool(description = "写入文件到指定路径")
    public String write(@ToolParam(description = "文件名称") String fileName,@ToolParam(description = "要往文件写入的内容") String content){
        String filePath = SAVE_FILE_DIR + "\\" + fileName;
        try {
            // 先创建目录  目录如果存在不创建 如果不存在创建
            FileUtil.mkdir(SAVE_FILE_DIR);
            FileUtil.writeUtf8String(content, filePath);
        }catch (IORuntimeException e){
            return "Error writing file：" + e.getMessage();
        }
        return "成功写入文件,写入到的路径是：" + filePath;
    }

}
